﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using INTEX.SCHEDULER;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class VKPLIST : UserControl
    {
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        } 

        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsLoaded = false;
        BindingListCollectionView cvVKP
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgVKP.ItemsSource);
            }
        }
        SETINTEXDB SetData
        {
            get
            {
                return vm.SetData;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetProd
        {
            get
            {
                return vm.SetProd;
            }
        }
        string KRS
        {
            get
            {
                return vm.KRS;
            }
            set
            {
                vm.KRS = value;
            }
        }
        string LFD
        {
            get
            {
                return vm.LFD;
            }
            set
            {
                vm.LFD = value;
            }
        }
        string POS
        {
            get
            {
                return vm.POS;
            }
            set
            {
                vm.POS = value;
            }
        }
        MainViewModel vm;
        public VKPLIST()
        {
       
            InitializeComponent();
            this.Loaded += Windows_Loaded;
            
        }
        public ITXSSPA Scheduler = new ITXSSPA();

        private readonly TaskScheduler _sync = TaskScheduler.FromCurrentSynchronizationContext();
        private NameScope pagescope = new System.Windows.NameScope();
        public delegate DataSet AsyncRunHandle(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null);
        string _sTemp = string.Empty;

        public void AsyncRun(string TaskType, object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null)
        {
            AsyncRunHandle fHandle = null;
            AsyncCallback callback = null;
            IAsyncResult aResult = null;
            WaitHandle waitHandle = null;
            switch (TaskType)
            {
                case "YGRPLANDB":
                    fHandle = new AsyncRunHandle(Scheduler.YGRPLANDB);
                    break;
                case "JLTPLANDB":
                    fHandle = new AsyncRunHandle(Scheduler.JLTPLANDB);
                    break; 
            }
            callback = new AsyncCallback(this.AsyncCallbackImpl);
            aResult = fHandle.BeginInvoke(p1, p2, p3, p4, p5, callback, fHandle);
            waitHandle = aResult.AsyncWaitHandle;
            waitHandle.WaitOne();
        }
        public void AsyncCallbackImpl(IAsyncResult run)
        {
            try
            {
                AsyncRunHandle handle = run.AsyncState as AsyncRunHandle;
                DataSet SResult = handle.EndInvoke(run);
                switch (handle.Method.Name)
                {
                    case "YGRPLANDB":
                    case "JLTPLANDB":
                        Task.Factory.StartNew(() => vm.LoadVKPList(), new CancellationTokenSource().Token, TaskCreationOptions.None, _sync).Wait();
                        break; 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
           
            IsLoaded = true;
            vm = (MainViewModel)this.DataContext;
            if (app.User.Trim() == "JLTV")
            {
                KRS = "J4";
                LFD = "001403";
                 
            }
            if (app.User.Trim() == "ITXV")
            {
                KRS = "14";
                LFD = "000155"; 
            }
            if (app.company.Trim() == "JLT")
            {               
                AsyncRun("JLTPLANDB", null, null, null);
            }
            if (app.company.Trim() == "YGR")
            {
                KRS = "14";
                LFD = "000155";
                AsyncRun("YGRPLANDB", null, null, null);
            }
            
            vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged; 
        }

        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwVKPOS == null)
                return;
            vm.dwVKP = dwVKPOS;
        }         
    }
}
